package com.yunyang.sciprt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import lombok.ToString;

@ToString
public class Contact {
	public String name;
	public String telphone;
	public String email;
	public String qq;
	public String company;
	public String account;
	public String account_bank;
	public String machine_fee;
	public String normal_fee = "";
	public String three_fee = "";
	public String other_fee = "";
	public String bounce_before;
	public String bounce_tel;
	public String stop_before;
	public String cinema_code;
	public String cinema_name;
	public String cinema_tel;
	public String address;

	private String regexFirst(Pattern p, String s) {
		Matcher m = p.matcher(s);
		if (m.find()) {
			return m.group(1);
		}
		return "";
	}

	private String regexFirst(String r, String s) {
		return regexFirst(Pattern.compile(r), s);
	}

	public Contact read(File file) throws IOException {
		InputStream is = new FileInputStream(file);
		XWPFDocument doc = new XWPFDocument(is);
		List<XWPFTable> tables = doc.getTables();
		List<XWPFTableRow> rows = tables.get(0).getRows();
		if(tables.size()>0){
			Pattern regex;
			Matcher m;

			List<XWPFTableCell> cells;
			cells = rows.get(3).getTableCells();
			name = cells.get(2).getText();
			telphone = cells.get(3).getText();
			email = cells.get(4).getText();
			qq = cells.get(5).getText();

			cells = rows.get(4).getTableCells();
			company = cells.get(3).getText();
			account = cells.get(5).getText().trim();

			cells = rows.get(5).getTableCells();
			account_bank = cells.get(3).getText().replaceAll("[ (）]", "");

			cells = rows.get(15).getTableCells();
			String s = cells.get(1).getText();
			regex = Pattern.compile("(\\d+)[\\s_]*元");
			machine_fee = regexFirst(regex, s);
			cells = rows.get(16).getTableCells();
			s = cells.get(2).getText();
			m = regex.matcher(s);
			if (m.find()) {
				normal_fee = m.group(1);
			}else{
				System.out.printf("%s:%s",file.getName(),s);
			}
			if (m.find()) {
				three_fee = m.group(1);
			}
			if (m.find()) {
				other_fee = m.group(1);
			}

			cells = rows.get(23).getTableCells();
			s = cells.get(1).getText();
			bounce_before = regexFirst("(\\d+)[\\s_]*分钟", s);
			bounce_tel = regexFirst("联系电话：[\\s_]*([\\d-]+)", s);
			stop_before = regexFirst("开场前[\\s_]*(\\d+)", s);
			cinema_code = regexFirst("编码[\\s_]*(\\d+)", s);

			cells = rows.get(26).getTableCells();
			s = cells.get(1).getText();
			cinema_name = regexFirst("店名：[\\s_]*([^\\s_]+)", s);
			cinema_tel = regexFirst("影城电话[\\s_]*([\\d-]+)", s);
			cells = rows.get(27).getTableCells();
			s = cells.get(1).getText();
			address = regexFirst("地址:[\\s_]*(\\S+)", s);
		}
		is.close();
		return this;
	}

	public void writeToCsv(OutputStream os) {
		try {
			os.write(name.getBytes());
			os.write(',');
			os.write(telphone.getBytes());
			os.write(',');
			os.write(email.getBytes());
			os.write(',');
			os.write(qq.getBytes());
			os.write(',');
			os.write(company.getBytes());
			os.write(',');
			os.write(account.getBytes());
			os.write(',');
			os.write(account_bank.getBytes());
			os.write(',');
			os.write(machine_fee.getBytes());
			os.write(',');
			os.write(normal_fee.getBytes());
			os.write(',');
			os.write(three_fee.getBytes());
			os.write(',');
			os.write(other_fee.getBytes());
			os.write(',');
			os.write(bounce_before.getBytes());
			os.write(',');
			os.write(bounce_tel.getBytes());
			os.write(',');
			os.write(stop_before.getBytes());
			os.write(',');
			os.write(cinema_code.getBytes());
			os.write(',');
			os.write(cinema_name.getBytes());
			os.write(',');
			os.write(cinema_tel.getBytes());
			os.write(',');
			os.write(address.getBytes());
			os.write('\n');
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
